# -*- coding: utf-8 -*-
# Copyright (c) 2013 Australian Government, Department of Sustainability, Environment, Water, Population and Communities
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

'''Generate documentation for MetaGeta modules'''

import os,sys,glob,re
from metageta import utilities
from epydoc.cli import cli
from epydoc import docparser
docparser.IMPORT_STAR_HANDLING='ignore'

args=[]
curdir=os.path.dirname(__file__)
args.extend(['--verbose'])
#args.extend(['--verbose']*3)
#args.extend(['--quiet']*3)
args.append('--name=MetaGETA (Metadata Gathering, Extraction and Transformation)')
#args.append('--css=white')     # Black on white, with blue highlights (similar to javadoc).
#args.append('--css=blue')      # Black on steel blue.
#args.append('--css=green')     # Black on green.
#args.append('--css=black')     # White on black, with blue highlights.
#args.append('--css=grayscale') # Grayscale black on white.
#args.append('--css=none') # No stylesheet
args.append('--css=%s'%os.path.join(curdir,'epydoc.css')) # ?.
args.append('--output=%s'%os.path.join(curdir,'doc','files'))
args.append('--html')
args.append('--show-private')
args.append('--inheritance=grouped')
args.append('--exclude=.*builtin.*')
args.append('--exclude=.*Tkinter.*')
#args.append('--no-imports')

args.append('%s'%os.path.join(curdir,'runcrawler.py'))
args.append('%s'%os.path.join(curdir,'runtransform.py'))

for f in glob.glob('%s'%os.path.join(curdir,'doc','files','*.*')):
    os.unlink(f)

args.append('%s'%os.path.join(curdir,'metageta'))
sys.argv.extend(args)
cli()

#Copy the index.html frameset file one level up
html=open('%s'%os.path.join(curdir,'doc','files','index.html')).read()
index=open('%s'%os.path.join(curdir,'doc','index.html'),'w')
index.write(html.replace('src="','src="files/'))
index.close()

#Get rid of the "int" and float classes cos I can't figure out how to exclude them from epydoc
toc='%s'%os.path.join(curdir,'doc','files','toc-everything.html')
intclass='%s'%os.path.join(curdir,'doc','files','int-class.html')
fltclass='%s'%os.path.join(curdir,'doc','files','float-class.html')
html=open(toc).read()
html=html.replace('    <a target="mainFrame" href="int-class.html"\n     >int</a><br />','')
html=html.replace('    <a target="mainFrame" href="float-class.html"\n     >float</a><br />','')
toc=open(toc,'w')
toc.write(html)
toc.close()
try:os.unlink(intclass)
except:pass
try:os.unlink(fltclass)
except:pass

#Get rid of "Generated by Epydoc..."
rex=re.compile('Generated by Epydoc.*on.*',re.I)
for f in glob.glob('%s'%os.path.join(curdir,'doc','files','*.html')):
    txt=open(f).read()
    match=rex.search(txt)
    if match:
        txt=txt.replace(txt[match.start():match.end()],'&nbsp;')
        open(f,'w').write(txt)

